package com.san.common.utils;

import java.util.Random;

/**
 * @author
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

	// 身份证星号方法
	public static String idMask(String idCardNum, int front, int end) {
		if (isEmpty(idCardNum)) {
			return idCardNum;
		}

		// 需要截取的长度不能大于身份证号长度
		if ((front + end) > idCardNum.length()) {
			return null;
		}
		// 需要截取的不能小于0
		if (front < 0 || end < 0) {
			return null;
		}
		// 计算*的数量
		int asteriskCount = idCardNum.length() - (front + end);
		StringBuffer asteriskStr = new StringBuffer();
		for (int i = 0; i < asteriskCount; i++) {
			asteriskStr.append("*");
		}
		String regex = "(\\w{" + String.valueOf(front) + "})(\\w+)(\\w{" + String.valueOf(end) + "})";
		return idCardNum.replaceAll(regex, "$1" + asteriskStr + "$3");
	}

	public static String getRandomString(int length) {
		String sources = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_@";
		Random random = new Random();
		char[] text = new char[length];
		for (int i = 0; i < length; i++) {
			text[i] = sources.charAt(random.nextInt(sources.length()));
		}
		return new String(text);
	}

	/**
	 * 全角转半角
	 * 
	 * @param str.
	 * @return 半角字符串
	 */
	public static final String full2Half(String str) {
		if (null == str || "".equals(str)) {
			return str;
		}

		char[] charArray = str.toCharArray();
        //对全角字符转换的char数组遍历
        for (int i = 0; i < charArray.length; ++i) {
            int charIntValue = (int) charArray[i];
            //如果符合转换关系,将对应下标之间减掉偏移量65248;如果是空格的话,直接做转换
            if (charIntValue >= 65281 && charIntValue <= 65374) {
                charArray[i] = (char) (charIntValue - 65248);
            } else if (charIntValue == 12288) {
                charArray[i] = (char) 32;
            }
        }
		return new String(charArray).trim();
	}
	
	/**
	 * 中文字符转英文字符
	 * @param str
	 * @return
	 */
	public static final String characterToEn(String str) {
		String[] regs = { "！", "，", "。", "；", "~", "《", "》", "（", "）", "？",
				"”", "｛", "｝", "“", "：", "【", "】", "”", "‘", "’", "!", ",",
				".", ";", "`", "<", ">", "(", ")", "?", "'", "{", "}", "\"",
				":", "{", "}", "\"", "\'", "\'" };
		for (int i = 0; i < regs.length / 2; i++) {
			str = str.replaceAll(regs[i], regs[i + regs.length / 2]);
		}
		return str;
	}
	
	/**
	 * 格式化字符，转半角，中文字符转英文字符，去空格
	 * @param input
	 * @return
	 */
	public static final String formatString(String input) {
		if (isEmpty(input)) {
			return input;
		}
		String str = full2Half(input);
		str = characterToEn(str);
		return str.trim();
	}
	
	/**
	 * 判断字符串是否为null或者空串
	 * @param str
	 * @return
	 */
	public static final boolean isEmpty(String str) {
		if (null == str || "".equals(str)) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * 加密身份证号码
	 * @param idCard
	 * @return
	 */
	public static final String encodeIdCard(String idCard) {
		if (isEmpty(idCard)) {
			return idCard;
		}
		return AESUtils.AESEncode(Constant.SECRET_KEY, idCard);
	}
	
	/**
	 * 解密身份证号码，并脱敏处理
	 * @param idCard
	 * @return
	 */
	public static final String decodeIdCard(String idCard) {
		if (isEmpty(idCard)) {
			return idCard;
		}
		String str = AESUtils.AESDecode(Constant.SECRET_KEY, idCard);
		return idMask(str, 3, 3);
	}
	
	/**
	 * null转""
	 * @param str
	 */
	public static final String nullFormat(String str) {
		if (isEmpty(str)) {
			return "";
		}
		return str;
	}
	
}
